import axios from "axios";

export interface Menu {
    id: string;
    parentId?: string;
    name?: string;
    icon?: string;
    type?: number;
    route?: string;
    filePath?: string;
    orderNumber?: number;
    url?: string;
    show?: boolean;
    children?: Menu[];
    path: string;
    parentPaths?: string[];
    authCode?: string;
    curVersion?: string;
}
export interface User {
    id: number;
    userName: string;
    nickName: string;
    phoneNumber: string;
    email: string;
    createDate: string;
    updateDate: string;
    avatar?: string | null;
    menus: Menu[];
    flatMenus: Menu[];
    avatarPath: string;
    authList: string[];
}

export interface PageData {
    data: User[];
    total: number;
}

const userService = {
    // 分页获取用户列表
    getUserListByPage: ({ current, pageSize }: { current: number; pageSize: number }, formData: any) => {
        return axios
            .get<PageData>("/midway/user/page", {
                params: {
                    page: current,
                    size: pageSize,
                    ...formData,
                },
            })
            .then(({ data }) => {
                return {
                    list: data.data,
                    total: data.total,
                };
            });
    },

    // 添加用户
    addUser: (data: User) => {
        return axios.post("/midway/user/create", data);
    },

    // 更新用户
    updateUser: (data: User) => {
        return axios.put("/midway/user/edit", data);
    },
    // 删除用户
    deleteUser: (id: number) => {
        return axios.delete(`/midway/user/${id}`);
    },
};

export default userService;
